//
//  TeamCloudAlbumTabBarVC.swift
//  YPWatermarkCamera
//
//  Created by 赵洋 on 2022/9/8.
//

import UIKit

// MARK: - 扩展WmcTeamCloudAlbumTabBarVC + WmcNavigatorProtocol

extension WmcTeamCloudAlbumTabBarVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.teamCloudAlbum]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        let vc = WmcTeamCloudAlbumTabBarVC()
        if let teamAblum = parms.context as? WmcMediaTeamAlbumModel {
            vc.teamAblum = teamAblum
        }
        return vc
    }
}

public extension WmcButtonItemType {
    /// 按钮类型
    enum TeamCloudAlbumTabBarItem {
        /// 首页
        case home

        /// 团队成员
        case teamMembers

        /// 分类相册
        case classifyAlbum

        /// 管理
        case management

        /// 自定义
        case custom(childVC: UIViewController,
                    childTitle: String,
                    normalImage: UIImage,
                    selectedImage: UIImage)
    }
}

// MARK: - [WmcTeamCloudAlbumTabBarVC]

class WmcTeamCloudAlbumTabBarVC: WmcCloudAlbumBaseTabBarVC {
    
    var albumHomeVC: WmcTeamCloudAlbumHomeVC?
    
    // Lifecycle

    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        if let hVC = albumHomeVC {
            hVC.tableView.emptyDelegate = nil
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        WmcNotificationManager.shared.addObserver(type: .clickedGotoShoot) { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.popToSpecificVC(vcClass: WmcHomeVC.self)
        }

        WmcNotificationManager.shared.addObserver(type: .memberQuitTeam) { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.popToSpecificVC(vcClass: WmvMyTeamVC.self)
        }

        NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "com.watermark.removeFromTeam"), object: nil, queue: .main) { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.navigationController?.popViewController(animated: true)
        }
    }

    // Open

    override open func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        super.tabBar(tabBar, didSelect: item)
    }

    // Internal

    var teamAblum: WmcMediaTeamAlbumModel? {
        didSet {
            // 现组装默认视图
            var defaultList: [WmcButtonItemType.TeamCloudAlbumTabBarItem] = [
                .home, .teamMembers, .classifyAlbum, .management,
            ]
            // 添加tabBar
            addItemList(caseList: defaultList)
        }
    }

    /// 转换成按钮实体
    func addItemList(caseList: [WmcButtonItemType.TeamCloudAlbumTabBarItem]) {
        caseList.forEach { cas in
            switch cas {
            case .home:
                let vm = WmcTeamCloudAlbumHomeVM()
                vm.teamAblum = teamAblum
                vm.title = teamAblum?.name ?? ""
                let homeVC = WmcTeamCloudAlbumHomeVC(vm)
                self.albumHomeVC = homeVC
                addCloudAlbumChildVC(homeVC,
                                     childTitle: "首页",
                                     normalImage: "team_ablum_home_icon".wmc_image,
                                     selectedImage: "team_ablum_home_icon_selected".wmc_image)
            case .teamMembers:
                let vm = WmcTeamCloudAlbumMembersVM()
                vm.teamAblum = teamAblum
                vm.title = "团队成员"
                addCloudAlbumChildVC(WmcTeamCloudAlbumMembersVC(vm),
                                     childTitle: "团队成员",
                                     normalImage: "team_member_normal".wmc_image,
                                     selectedImage: "team_member_selected".wmc_image)
            case .classifyAlbum:

                let vm = WmcTeamCloudCategriesAlbumVM()
                vm.teamAblum = teamAblum
                vm.title = "分类相册"
                // 我看团队分类相册和个人有区别，要是想用一个VC也行，分开做VC的话就把这里VC改成新分类相册VC即可
                addCloudAlbumChildVC(WmcTeamCloudCategriesAlbumVC(vm),
                                     childTitle: "分类相册",
                                     normalImage: "cloud_tabbar_classify_album_no".wmc_image,
                                     selectedImage: "cloud_tabbar_classify_album_se".wmc_image)
            case .management:
                let vm = WmcTeamCloudAlbumManagementVM()
                vm.title = "管理"
                vm.teamAblum = teamAblum
                addCloudAlbumChildVC(WmcTeamCloudAlbumManagementVC(vm),
                                     childTitle: "管理",
                                     normalImage: "cloud_tabbar_setting_no".wmc_image,
                                     selectedImage: "cloud_tabbar_setting_se".wmc_image)
            case let .custom(childVC: childVC, childTitle: childTitle, normalImage: normalImage, selectedImage: selectedImage):
                addCloudAlbumChildVC(childVC,
                                     childTitle: childTitle,
                                     normalImage: normalImage,
                                     selectedImage: selectedImage)
            }
        }
    }
}
